package cn.qiba.sms.kit;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.github.qcloudsms.*;
import com.github.qcloudsms.httpclient.HTTPException;
import org.aspectj.lang.annotation.Before;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class TencentSmsKit {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Value("${sms.tencent.appid}")
    private Integer appid;
    @Value("${sms.tencent.appkey}")
    private String appkey;

    /**
     * 腾讯云国内短信发送  - 单发
     * @param phoneNumber
     * @param signName
     * @param templateId
     * @param templateParams
     * @return
     * @throws ClientException
     */
    public SmsSingleSenderResult sendSms(String phoneNumber, String signName, String templateId, String[] templateParams) throws ClientException {
        try {
            SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
            SmsSingleSenderResult result = ssender.sendWithParam("86", phoneNumber,Integer.parseInt(templateId), templateParams, signName, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            return result;
        } catch (HTTPException e) {
            // HTTP响应码错误
            logger.error("腾讯云短信发送失败",e);
        } catch (JSONException e) {
            // json解析错误
            logger.error("腾讯云短信发送失败",e);
        } catch (IOException e) {
            // 网络IO错误
            logger.error("腾讯云短信发送失败",e);
        }
        return null;
    }


    /**
     * 腾讯云国内短信发送  - 群发
     * @param phoneNumbers
     * @param signName
     * @param templateId
     * @param templateParams
     * @return
     * @throws ClientException
     */
    public SmsMultiSenderResult sendMultiSms(String[] phoneNumbers, String signName, String templateId, String[] templateParams) throws ClientException {

        try {
            SmsMultiSender msender = new SmsMultiSender(appid, appkey);
            return msender.sendWithParam("86", phoneNumbers,Integer.parseInt(templateId), templateParams, signName, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
        } catch (HTTPException e) {
            // HTTP响应码错误
            logger.error("腾讯云短信发送失败",e);
        } catch (JSONException e) {
            // json解析错误
            logger.error("腾讯云短信发送失败",e);
        } catch (IOException e) {
            // 网络IO错误
            logger.error("腾讯云短信发送失败",e);
        }
        return null;
    }

}
